//
//  SZViewController.m
//  SZBarrageManager
//
//  Created by isstarz@163.com on 09/07/2017.
//  Copyright (c) 2017 isstarz@163.com. All rights reserved.
//

#import "SZViewController.h"
#import <SZBarrageManager/SZBarrageManager.h>
#import <SZBarrageManager/SZChatMsgBarrage.h>
#import <SZBarrageManager/SZUEnterBarrage.h>


@interface SZViewController ()<UITableViewDelegate, UITableViewDataSource>


@property (weak, nonatomic) IBOutlet UITextField *roodIDField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic)   NSMutableArray  *barrages;

@end

@implementation SZViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.barrages = [NSMutableArray array];
    
    self.roodIDField.text = @"271934";
    
    [self setupNotifications];
    

}

- (IBAction)acitonEnterRoom:(id)sender {
    
    [[SZBarrageManager shared] joinRoomWithRoomID:self.roodIDField.text];
}
- (IBAction)actionExitRoom:(id)sender {
    
    [[SZBarrageManager shared] exitRoom];
}

#pragma mark - 通知管理
- (void)setupNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(chatMsgBarrageNotification:) name:SZChatMsgBarrageNotification object:[SZBarrageManager shared]];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(uenterBarrageNotification:) name:SZUEnterBarrageNotification object:[SZBarrageManager shared]];
}

- (void)teardownNotifications {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 通知回调
- (void)chatMsgBarrageNotification:(NSNotification *)noti
{
    SZChatMsgBarrage *barrage = noti.userInfo[kBarrage];
    [self.barrages addObject:barrage];
    
    [self refresh];
}

- (void)uenterBarrageNotification:(NSNotification *)noti
{
    SZUEnterBarrage *barrage = noti.userInfo[kBarrage];
    [self.barrages addObject:barrage];
    
    [self refresh];
    
}

- (void)refresh {
    
    [self.tableView reloadData];
    
   
    if (self.tableView.contentSize.height > self.tableView.bounds.size.height) {
        [self.tableView setContentOffset:CGPointMake(0, self.tableView.contentSize.height - self.tableView.bounds.size.height) animated:YES];
    }
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.barrages.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    
    id obj = self.barrages[indexPath.row];
    
    if ([obj isKindOfClass:[SZChatMsgBarrage class]]) {
        
        SZChatMsgBarrage *barrage = obj;
        
        cell.textLabel.text = barrage.nn;
        cell.detailTextLabel.text = barrage.txt;
    } else if ([obj isKindOfClass:[SZUEnterBarrage class]]) {
        
        SZUEnterBarrage *barrage = obj;
        cell.textLabel.text = barrage.nn;
        
        cell.detailTextLabel.text = @"进入本房间";
    }
    
    return cell;
}




@end
